package com.ccp.dev.workflow.model;

import com.alibaba.fastjson.JSONArray;
import com.ccp.dev.core.basic.model.BaseModel;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.workflow.model.bpm.ConditionJsonStruct;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.ArrayList;
import java.util.List;


/**
 * 对象功能:流程节点用户选择条件
 * @author jiayueqiao
 */
@XmlRootElement(name = "bpmUserCondition")
@XmlAccessorType(XmlAccessType.NONE)
public class BpmUserCondition extends BaseModel<BpmUserCondition> implements Cloneable,Comparable<BpmUserCondition>
{
	/**
	 * 等于
	 */
	public static final  short OPERATE_TYPE_EQUAL= 0;
	/**
	 * 不等于
	 */
	public static final  short OPERATE_TYPE_UNEQUAL= 1;
	/**
	 * 大于
	 */
	public static final  short OPERATE_TYPE_MORE_THAN = 2;
	/**
	 * 大于等于
	 */
	public static final  short OPERATE_TYPE_MORE_EQUAL_THAN = 3;
	/**
	 * 小于
	 */
	public static final  short OPERATE_TYPE_LESS_THAN = 4;
	/**
	 * 小于等于
	 */
	public static final  short OPERATE_TYPE_LESS_EQUAL_THAN = 5;
	/**
	 * like
	 */
	public static final  short OPERATE_TYPE_LIKE = 6;
	/**
	 * unlike
	 */
	public static final  short OPERATE_TYPE_UNLIKE = 7;

	/**
	 * 变量为数字类型
	 */
	public static final  String VARIABLE_TYPE_NUMBER = "number";
	/**
	 * 变量为日期类型
	 */
	public static final  String VARIABLE_TYPE_DATE = "date";
	/**
	 * 变量为字符串类型
	 */
	public static final  String VARIABLE_TYPE_STRING= "varchar";
	
	/**
	 * 执行人员规则 
	 */
	public static final  int CONDITION_TYPE_EXEUSER= 0;
	/**
	 * 抄送人员规则
	 */
	public static final  int CONDITION_TYPE_COPYUSER= 1; 
	/** 
	 * 消息任务接收人规则设置 正常邮件 
	 */
	public static final  int CONDITION_TYPE_MSG_MAIL_RECEIVER	= 3;
	/**
	 * 消息任务接收人规则设置 抄送
	 */
	public static final  int CONDITION_TYPE_MSG_MAIL_COPYTO = 4;
	/**
	 * 消息任务接收人规则设置 密送
	 */
	public static final  int CONDITION_TYPE_MSG_MAIL_BCC = 5;
	/**
	 * 消息任务接收人规则设置 内部消息
	 */
	public static final  int CONDITION_TYPE_MSG_INNER_RECEIVER = 6;
	/**
	 * 消息任务接收人规则设置 手机短信
	 */
	public static final  int CONDITION_TYPE_MSG_MOBILE_RECEIVER = 7;
	/**
	 * 触发新流程执行人
	 */
	public static final  int CONDITION_TYPE_TRIGGER_NEWFLOW_STARTUSER = 8;
	/**
	 * 触发新流程NodeId前缀
	 */
	public static final  String TRIGGER_NEWFLOW_STARTUSER_NODE_PRDFIX	 = "newFlow_";


	/**
	 * ID
	 */
	@XmlAttribute
	protected String  id;
	/**
	 * oldId
	 * 仅用于导入时与bpmNodeUser.getConditionId循环比对使用
	 * 目的：在导入BPMNodeUser时，根据oldId与bpmNodeUser.conditionId比对，再将新id赋值给conditionId
	 */
	protected String  oldId;

	public String getOldId() {
		return oldId;
	}

	public void setOldId(String oldId) {
		this.oldId = oldId;
	}

	/**
	 * 流程定义id
	 */
	@XmlAttribute
	protected String  procDefId="";

	/**
	 * 节点id
	 */
	@XmlAttribute
	protected String  nodeId="";
	/**
	 * 条件规则
	 */
	@XmlAttribute
	protected String  condition="";

	/**
	 * SN 排序
	 */
	@XmlAttribute
	protected Long  sn=0L;
	/**
	 * 表单变量(标识所挂表单是否发生变化)
	 */
	@XmlAttribute
	protected String formIdentity="";
	/**
	 * 规则名称
	 */
	@XmlAttribute
	protected String  conditionName="";
	/**
	 * 规则显示字段
	 */
	@XmlAttribute
	protected String  conditionShow="" ;
	/**
	 * 节点设置id
	 */
	@XmlAttribute
	protected String  setId;
	/**
	 * 规则的类型 
	 * 0：执行人员规则 
	 * 1：抄送人员规则,
	 */
	@XmlAttribute
	protected Integer conditionType=0;

	/**
	 * 分组号
	 */
	@XmlAttribute
	protected Integer groupNo=1;

	/**
	 * 父流程定义ID
	 */
	@XmlAttribute
	protected String parentProcDefId = "";
	
	
	public String getConditionShow() {
		return conditionShow;
	}	
	public void setConditionShow(String conditionShow) {
		this.conditionShow = conditionShow;
	}
	public void setId(String id)
	{
		this.id = id;
	}
	/**
	 * 返回 ID
	 * @return String
	 */
	public String getId()
	{
		return this.id;
	}
	public void setProcDefId(String procDefId)
	{
		this.procDefId = procDefId;
	}
	/**
	 * 返回 PROCDEFID
	 * @return String
	 */
	public String getProcDefId()
	{
		return this.procDefId;
	}
	public void setNodeId(String nodeId)
	{
		this.nodeId = nodeId;
	}
	/**
	 * 返回 NODEID
	 * @return String
	 */
	public String getNodeId()
	{
		return this.nodeId;
	}
	public void setCondition(String condition)
	{
		this.condition = condition;
	}


   	public String getCondition() {
		return condition;
	}
	public Long getSn() {
		return sn;
	}
	public void setSn(Long sn) {
		this.sn = sn;
	}
	public String getFormIdentity() {
		return formIdentity;
	}
	public void setFormIdentity(String formIdentity) {
		this.formIdentity = formIdentity;
	}
	public String getConditionName() {
		return conditionName;
	}
	public void setConditionName(String conditionName) {
		this.conditionName = conditionName;
	}
	public String getSetId() {
		return setId;
	}
	public void setSetId(String setId) {
		this.setId = setId;
	}
	public Integer getConditionType() {
		return conditionType;
	}
	public void setConditionType(Integer conditionType) {
		this.conditionType = conditionType;
	}
	public Integer getGroupNo() {
		return groupNo;
	}
	public void setGroupNo(Integer groupNo) {
		this.groupNo = groupNo;
	}

	public String getParentProcDefId() {
		return parentProcDefId;
	}
	public void setParentProcDefId(String parentProcDefId) {
		this.parentProcDefId = parentProcDefId;
	}
	/**
	 * @see Object#equals(Object)
	 */
	@Override
	public boolean equals(Object object)
	{
		if (!(object instanceof BpmUserCondition))
		{
			return false;
		}
		BpmUserCondition rhs = (BpmUserCondition) object;
		return new EqualsBuilder()
		.append(this.id, rhs.id)

		.isEquals();
	}

	/**
	 * @see Object#hashCode()
	 */
	@Override
	public int hashCode()
	{
		return new HashCodeBuilder(-82280557, -700257973)
		.append(this.id)
		.append(this.procDefId)
		.append(this.nodeId)
		.append(this.condition)
		.append(this.sn)

		.append(this.conditionName)
			.append(this.conditionShow)
				.append(this.setId)
				.append(this.groupNo)
				.append(this.parentProcDefId)
		.toHashCode();
	}

	/**
	 * @see Object#toString()
	 */
	@Override
	public String toString()
	{
		return new ToStringBuilder(this)
		.append("id", this.id)
		.append("procDefId", this.procDefId)
		.append("nodeId", this.nodeId)
		.append("condition", this.condition) 
		.append("sn", this.sn)
		.append("conditionName",this.conditionName)
		.append("conditionShow",this.conditionShow)
		.append("setId",this.setId)
		.append("groupNo",this.groupNo)
		.toString();
	}

	@Override
	public Object clone() {
		BpmUserCondition obj = null;
		try {
			obj = (BpmUserCondition) super.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return obj;
	}
  
	public List<ConditionJsonStruct> getConditionJson(){
		if(StringUtil.isEmpty(this.condition)){
			return new ArrayList<ConditionJsonStruct>();
		}
		List<ConditionJsonStruct> conditionJson = JSONArray.parseArray(this.condition, ConditionJsonStruct.class);
		return conditionJson;
	}


	@Override
    public int compareTo(BpmUserCondition condition) {
		if(this.getGroupNo()==null){
			return -1;
		}else if(condition.getGroupNo()==null){
			return 1;
		}else{
			return this.getGroupNo().compareTo(condition.getGroupNo());
		}
	}
}